$(function(){
    $("input").on("click",function(){
        var name=$(this).attr("class");
        switch(name){
            case "listTh":
            var prevAll=$(this).parents(".contbox3").prevAll();
            var len=prevAll.length;
            for(var i=0;i<len;i++){
                if(prevAll[i].classList[1]=="contbox2"){
                    $(this).parents(".contbox3").prevAll().eq(i).find($(".listT")).prop("checked",true);
                    break;
                }
            }
            for(var i=0;i<len;i++){
                if(prevAll[i].classList[1]=="contbox1"){
                    prevAll[i].children[0].children[0].children[0].checked=true;
                    break;
                }
            };
            break;
            
            case "listT":
            if($(this).prop("checked")==true){
                var prevAll=$(this).parents(".contbox2").prevAll();
                var len=prevAll.length;
                for(var i=0;i<len;i++){
                    if(prevAll[i].classList[1]=="contbox1"){
                        prevAll[i].children[0].children[0].children[0].checked=true;
                        break;
                    }
                };
            }else{
                var nextAll=$(this).parents(".contbox2").nextAll();
                var len=nextAll.length;
                for(var i=0;i<len;i++){
                    if(nextAll[i].classList[1]!="contbox2"||nextAll[i].classList[1]!="contbox1"){
                        nextAll[i].children[0].children[0].children[0].checked=false;
                    }
                }
            };
            break;

            case "listO":
            if($(this).prop("checked")!=true){
                var nextAll=$(this).parents(".contbox1").nextAll();
                var len=nextAll.length;
                for(var i=0;i<len;i++){
                    if(nextAll[i].classList[1]!="contbox1"){
                        nextAll[i].children[0].children[0].children[0].checked=false;
                    }else{
                        break;
                    }
                }
            };
            break;
        }  
    });

    $(".btn-save").on("click",function(){
        var btngroup=$("input[type=checkbox]:checked"),
        arr=[];len=btngroup.length;
        var group_name=$(".group_name").val();
        var id=$(".hide-id").val();

        for(var i=0;i<len;i++){
            arr.push(btngroup[i].value);
        }
        if(group_name!=""){
            ajax("post","/permission/update-group",{group_name:group_name,group_jurisd_id:arr,group_id:id},
            function success(data){
                if(data.code==200){
                    layer.msg(data.msg);
                    setTimeout(function(){
                        location.href="/permission/index";
                    },1200)
                }
            },
            function error(data){
                layer.msg(data.msg)
            }
        )
        }else{
            layer.msg("请填写权限名称")
        }
    })
})